<?php

class User extends CI_Model {

    function index() {
        $q = $this->db->get('user');
        return $q;
    }

    function create($name, $username, $email, $password, $last_name, $user_level, $date) {
        $data = array(
            'name' => $name,
            'username' => $username,
            'email' => $email,
            'password' => $password,
            'last_name' => $last_name,
            'user_level' => $user_level,
            'date_created' => $date,
        );
        $this->db->insert('user', $data);
        redirect('user_access/index');
    }

    function edit($id) {
        $this->db->where('id', $id);
        $q = $this->db->get('user');
        return $q->result();
    }

    function update($id, $name, $username, $email, $password, $last_name, $user_level) {
        if ($password == null) {
            $data = array(
                'name' => $name,
                'username' => $username,
                'email' => $email,
                'last_name' => $last_name,
                'user_level' => $user_level,
            );
        } else {
            $data = array(
                'name' => $name,
                'username' => $username,
                'email' => $email,
                'password' => $password,
                'last_name' => $last_name,
                'user_level' => $user_level,
            );
        }
        $this->db->where('id', $id);
        $this->db->update('user', $data);
        redirect('user_access/index');
    }

    function login($username, $password) {
        $this->db->select('id, username, password');
        $this->db->from('user');
        $this->db->where('username = ' . "'" . $username . "'");
        $this->db->where('password = ' . "'" . md5($password) . "'");
        $this->db->limit(1);

        $query = $this->db->get();

        if ($query->num_rows() == 1) {
            return $query->result();
        } else {
            return false;
        }
    }

    function cek($id) {
        $this->db->where('id', $id);
        $q = $this->db->get('user');

        if ($q->num_rows() > 0) {
            foreach ($q->result() as $row) {
                $cek = $row->user_level;
            }
        } else {
            $cek = 0;
        }

        return $cek;
    }

    function delete($id) {
        $this->db->where('id', $id);
        $this->db->delete('user');
        redirect('user_access/index');
    }
    
    function get_admin($id){
        $this->db->where('id', $id);
        $q = $this->db->get('user');
        if(!$q->result()==null){
        foreach ($q->result() as $row ){
            $data = $row->name;
        }
        return $data;
        }
        return false;
    }

}

?>
